package com.example.service.course;

import com.example.dao.course.CourseclassDao;
import com.example.pojo.ElementTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CourseclassServceImp implements CourseclassServce{
    @Autowired
    private CourseclassDao courseclassDao;
    @Override
    public List<ElementTree> getTreeData() {
        List<ElementTree> all = courseclassDao.getAll();
        List<ElementTree> tree = getTree(all, 0);

        return tree;
    }

    private List<ElementTree> getTree(List<ElementTree> all,int parentId){
        List<ElementTree> list = new ArrayList<>();
        for (ElementTree elementTree : all) {
            if (elementTree.getParentId()==parentId){
                ElementTree e = new ElementTree();
                e.setId(elementTree.getId());
                e.setName(elementTree.getName());
                e.setChildren(getTree(all,elementTree.getId()));
                list.add(e);
            }
        }
        return list;
    }
}